package com.share.netdisk.reclaim.controller;

import com.share.constant.Result;
import com.share.netdisk.reclaim.model.Reclaim;
import com.share.netdisk.reclaim.service.ReclaimService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Value;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/reclaim")
public class ReclaimController {

	@Autowired
	private ReclaimService reclaimService;

	@Value("${hdfs.uri}")
	private String hdfsUri;

	//根据用户id展示回收站列表
	@GetMapping("/user/{userId}")
	public List<Reclaim> getFilesByUserId(@PathVariable Integer userId) {
		return reclaimService.getFilesByUserId(userId);
	}

	// 删除回收站的数据，多个reclaimId用逗号隔开
	@DeleteMapping("/reclaim/delete")
	public Result deleteReclaim(@RequestParam(value = "reclaims", required = true) String reclaimIds) {
		if (reclaimIds == null || reclaimIds.trim().isEmpty()) {
			return Result.fail(400, "Parameter 'reclaims' is missing or empty", null);
		}

		try {
			// 将逗号分隔的字符串转换为整数列表
			List<Integer> ids = Arrays.stream(reclaimIds.split(","))
					.map(String::trim)
					.map(Integer::parseInt)
					.collect(Collectors.toList());

			// 批量永久删除
			boolean result = reclaimService.permanentlyDeleteReclaims(ids);
			if (!result) {
				return Result.fail(404, "No files were found for the provided IDs", null);
			}
			return Result.ok("Files deleted successfully: " + ids);

		} catch (NumberFormatException e) {
			return Result.fail(400, "Invalid reclaim ID format: " + e.getMessage(), null);
		} catch (Exception e) {
			return Result.fail(500, "An error occurred while deleting files: " + e.getMessage(), null);
		}
	}



	//删除所有的数据
	@GetMapping("/deleteAll/{userId}")
	public boolean deleteAllReclaim(@PathVariable Integer userId) {
		return reclaimService.removeByMap(null);
	}
}
